﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;

namespace BookStore.Solution.Presentation
{
    public partial class ucStatistic : UserControl
    {
        public ucStatistic()
        {
            InitializeComponent();
        }
        BillManager billManager = new BillManager();
        EntryBillManager entrybillManager = new EntryBillManager();
        BillDetailManager billDetail = new BillDetailManager();
        EntryBillDetailManager entryBillDetail = new EntryBillDetailManager();
        private void CreateTable(string type)
        {
            grvStatisticLits.AutoGenerateColumns = false;
            grvStatisticLits.AutoResizeColumns();
            grvStatisticLits.AutoResizeRows();
            DataGridViewTextBoxColumn idColumn1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn idColumn2 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn idColumn3 = new DataGridViewTextBoxColumn();
            if (type.CompareTo("Doanhthu") == 0)
            {
                DataGridViewTextBoxColumn idColumn4 = new DataGridViewTextBoxColumn();
                idColumn4.HeaderText = "Tiền cuối ngày";
                idColumn4.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                idColumn4.Resizable = DataGridViewTriState.False;
                idColumn4.ReadOnly = true;
                grvStatisticLits.Columns.Add(idColumn4);
            }
            idColumn2.HeaderText = "Tiền thu";
            idColumn3.HeaderText = "Tiền chi";
            idColumn1.HeaderText = "Ngày";

            idColumn1.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            idColumn1.Resizable = DataGridViewTriState.False;
            idColumn1.ReadOnly = true;
            grvStatisticLits.Columns.Add(idColumn1);
            idColumn2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            idColumn2.Resizable = DataGridViewTriState.False;
            idColumn2.ReadOnly = true;
            grvStatisticLits.Columns.Add(idColumn2);

            idColumn3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            idColumn3.Resizable = DataGridViewTriState.False;
            idColumn3.ReadOnly = true;
            grvStatisticLits.Columns.Add(idColumn3);
        }
        private void btnFind_Click(object sender, EventArgs e)
        {

            grvStatisticLits.Columns.Clear();

            if (rdThongKedoanhThu.Checked == true)
            {
                CreateTable("Doanhthu");
                StatisticRevenues();
            }
            if (rdThongKeBanHang.Checked == true)
            {
                CreateTable("BanHang");
                StatistcSales();
            }
            if (rdThongKeNhapHang.Checked == true)
            {
                CreateTable("NhapHang");
                StatisticImport();
            }

        }
        private void StatistcSales()
        {
            BillManager billManager = new BillManager();
            DateTime dt = dtpStartDate.Value;
            EntryBillManager entrybillManager = new EntryBillManager();
            grvStatisticLits.Columns[1].HeaderText = "Số lượng sản phẩm bán";
            switch (cbbType.SelectedIndex)
            {
                case 0:
                    grvStatisticLits.Columns[0].HeaderText = "Ngày";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền theo ngày";
                    while (dt <= tdpEndDate.Value)
                    {
                        int Quantity = 0;
                        long money = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date);
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Date.ToShortDateString(), Quantity, money);
                        dt = dt.AddDays(1);
                    }
                    break;
                case 1:

                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Tháng";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền theo tháng";
                    while (dt.Month <= tdpEndDate.Value.Month && dt.Year <= tdpEndDate.Value.Year || dt.Year < tdpEndDate.Value.Year)
                    {
                        int Quantity = 0;
                        long money = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date, "Month");
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Month + "-" + dt.Year, Quantity, money);
                        dt = dt.AddMonths(1);
                    }
                    break;
                case 2:
                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Năm";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền theo năm";
                    while (dt.Year <= tdpEndDate.Value.Year)
                    {
                        int Quantity = 0;
                        long money = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date, "Year");
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Year, Quantity, money);
                        dt = dt.AddYears(1);
                    }
                    break;
                default:
                    break;
            }
        }
        private void StatisticRevenues()
        {
            BillManager billManager = new BillManager();
            DateTime dt = dtpStartDate.Value;
            EntryBillManager entrybillManager = new EntryBillManager();
            grvStatisticLits.Columns[1].HeaderText = "Tiền thu";
            grvStatisticLits.Columns[2].HeaderText = "Tiền chi";
            switch (cbbType.SelectedIndex)
            {
                case 0:
                    grvStatisticLits.Columns[0].HeaderText = "Ngày";
                    grvStatisticLits.Columns[3].HeaderText = "Tổng thu chi ngày";
                    while (dt <= tdpEndDate.Value)
                    {
                        int Quantity = 0, Quantity1 = 0;
                        long money = 0, money1 = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date);
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date);
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity1, ref money1);
                        }
                        grvStatisticLits.Rows.Add(dt.Date.ToShortDateString(), money, money1, money - money1);
                        dt = dt.AddDays(1);
                    }
                    break;
                case 1:
                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Tháng";
                    grvStatisticLits.Columns[3].HeaderText = "Tổng thu chi tháng";
                    while (dt.Month <= tdpEndDate.Value.Month && dt.Year <= tdpEndDate.Value.Year || dt.Year < tdpEndDate.Value.Year)
                    {
                        int Quantity = 0, Quantity1 = 0;
                        long money = 0, money1 = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date, "Month");
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date, "Month");
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity1, ref money1);
                        }
                        grvStatisticLits.Rows.Add(dt.Date.Month + "-" + dt.Year, money, money1, money - money1);
                        dt = dt.AddMonths(1);
                    }
                    break;
                case 2:

                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Năm";
                    grvStatisticLits.Columns[3].HeaderText = "Tổng thu chi năm";
                    while (dt.Year <= tdpEndDate.Value.Year)
                    {
                        int Quantity = 0, Quantity1 = 0;
                        long money = 0, money1 = 0;
                        DataTable tb = billManager.SearchByDay(dt.Date, "Year");
                        if (tb != null)
                        {
                            getDataBillByDay(tb, ref Quantity, ref money);
                        }
                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date, "Year");
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity1, ref money1);
                        }
                        grvStatisticLits.Rows.Add(dt.Year, money, money1, money - money1);
                        dt = dt.AddYears(1);
                    }
                    break;
                default:
                    break;
            }
        }
        private void StatisticImport()
        {
            BillManager billManager = new BillManager();
            DateTime dt = dtpStartDate.Value;
            EntryBillManager entrybillManager = new EntryBillManager();

            grvStatisticLits.Columns[1].HeaderText = "Số lượng sản phẩm đã nhập";
            switch (cbbType.SelectedIndex)
            {
                case 0:
                    grvStatisticLits.Columns[0].HeaderText = "Ngày";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền chi theo ngày";
                    while (dt <= tdpEndDate.Value)
                    {
                        int Quantity = 0;
                        long money = 0;
                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date);
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Date.ToShortDateString(), Quantity, money);
                        dt = dt.AddDays(1);
                    }
                    break;
                case 1:
                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Tháng";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền chi theo tháng";
                    while (dt.Month <= tdpEndDate.Value.Month && dt.Year <= tdpEndDate.Value.Year || dt.Year < tdpEndDate.Value.Year)
                    {
                        int Quantity = 0;
                        long money = 0;

                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date, "Month");
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Date.Month + "-" + dt.Year, Quantity, money);

                        dt = dt.AddMonths(1);
                    }
                    break;
                case 2:

                    dt = dtpStartDate.Value;
                    grvStatisticLits.Columns[0].HeaderText = "Năm";
                    grvStatisticLits.Columns[2].HeaderText = "Tổng tiền chi theo năm";
                    while (dt.Year <= tdpEndDate.Value.Year)
                    {
                        int Quantity = 0;
                        long money = 0;
                        DataTable tb1 = entrybillManager.SearchByDay(dt.Date, "Year");
                        if (tb1 != null)
                        {
                            getDataEntryBillByDay(tb1, ref Quantity, ref money);
                        }
                        grvStatisticLits.Rows.Add(dt.Year, Quantity, money);
                        dt = dt.AddYears(1);
                    }
                    break;
                default:
                    break;
            }
        }

        private void getDataBillByDay(DataTable tb, ref int Quantity, ref long money)
        {
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                DataTable tbBillDetail = billDetail.SearchID(int.Parse(dr["BillID"].ToString()));
                for (int j = 0; j < tbBillDetail.Rows.Count; j++)
                {
                    DataRow dtr = tbBillDetail.Rows[j];
                    Quantity += int.Parse(dtr["Quantity"].ToString());
                    money += int.Parse(dtr["Quantity"].ToString()) * int.Parse(dtr["UnitPrice"].ToString());
                }
            }
        }

        private void getDataEntryBillByDay(DataTable tb, ref int Quantity, ref long money)
        {
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                DataRow dr = tb.Rows[i];
                DataTable tbEntryBillDetail = entryBillDetail.SearchID(int.Parse(dr["EntryBillID"].ToString()));
                for (int j = 0; j < tbEntryBillDetail.Rows.Count; j++)
                {
                    DataRow dtr = tbEntryBillDetail.Rows[j];
                    Quantity += int.Parse(dtr["Quatity"].ToString());
                    money += int.Parse(dtr["Quatity"].ToString()) * int.Parse(dtr["UnitPrice"].ToString());
                }
            }
        }

        private void ucStatistic_Load(object sender, EventArgs e)
        {

        }

        private void grStatistic_Enter(object sender, EventArgs e)
        {

        }
    }
}
